#include "Scene.h"
#include <algorithm>

Scene::Scene(void)
{
}


Scene::~Scene(void)
{
	UnLoad();
}


bool Scene::Init()
{
	m_currentState = SCENE_STATE_INIT;

	return true;
}


void Scene::Update()
{
	switch(m_currentState)
	{
	case SCENE_STATE_INIT:
		{
			m_currentState = SCENE_STATE_LOAD;
			m_bLoadOver = false;
		}
		break;
	case SCENE_STATE_LOAD:
		{
			Load();
			if(m_bLoadOver == true)
				m_currentState = SCENE_STATE_RUN;
		}
		break;
	case SCENE_STATE_RUN:
		{
			ObjectList_Update();
			ObjectList_Sort();
		}
		break;
	}
}


void Scene::Paint()
{
	switch(m_currentState)
	{
	case SCENE_STATE_INIT:
		{
		}
		break;
	case SCENE_STATE_LOAD:
		{
		}
		break;
	case SCENE_STATE_RUN:
		{
			ObjectList_Paint();
		}
		break;
	}
}


void Scene::Load()
{
	ObjAxis3* axis3 = ObjFactory::GetInstance()->CreateAxis3(Point3f(0.0, 0.0, 0.0), 3.0);
	m_objList.push_back(axis3);
	ObjWireCube* cube = ObjFactory::GetInstance()->CreateWireCube(Point3f(0.0, 0.0, 0.0), 3.0, 0xffffffff);
	m_objList.push_back(cube);

	m_bLoadOver = true;
}


void Scene::UnLoad()
{
	for(ObjectList::iterator it = m_objList.begin(); it != m_objList.end(); ++it)
	{
		delete (*it);
	}
	m_objList.clear();
}


void Scene::ObjectList_Update()
{
	for(ObjectList::iterator it = m_objList.begin(); it != m_objList.end(); ++it)
	{
		(*it)->Update();
	}
}


int IsFarThan(CommonObject* lp, CommonObject* rp)
{
	if(lp->m_viewPriority = rp->m_viewPriority)
	{
		if( lp->GetDistance(camera) > rp->GetDistance(camera) )
			return 1;
		else
			return 0;
	}
	else
	{
		if( lp->m_viewPriority > rp->m_viewPriority )
			return 1;
		else
			return 0;
	}
}


void Scene::ObjectList_Sort()
{
	m_objList.sort(IsFarThan);
}


void Scene::ObjectList_Paint()
{
	for(ObjectList::iterator it = m_objList.begin(); it != m_objList.end(); ++it)
	{
		(*it)->Paint();
	}
}